package com.yuzhitong.shapi.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.yuzhitong.shapi.R;
import com.yuzhitong.shapi.activity.MovieDetailActivity;
import com.yuzhitong.shapi.activity.PlayNewActivity;
import com.yuzhitong.shapi.base.BaseMvpFragment;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.base.recycler.BaseAdapter;
import com.yuzhitong.shapi.base.recycler.BaseViewHolder;
import com.yuzhitong.shapi.bean.MovieBean;
import com.yuzhitong.shapi.contract.SearchCategoryContract;
import com.yuzhitong.shapi.presenter.SearchCategoryPresenter;
import com.yuzhitong.shapi.util.AppUtil;
import com.yuzhitong.shapi.util.GlideUtil;
import com.yuzhitong.shapi.util.LoggerUtil;
import com.yuzhitong.shapi.util.UiUtil;
import com.yuzhitong.shapi.widget.ScoreView;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/5/10  20:14
 * Description:
 * 根据 name 条件搜索
 */
public class SearchCategoryFragment extends BaseMvpFragment<SearchCategoryPresenter> implements SearchCategoryContract.View {


    private SwipeRefreshLayout srlRefresh;
    private RecyclerView rvSearchCategory;

    private TextView bottomTextView;

    private BaseAdapter<MovieBean> adapter;
    private int pageNumber = 0;

    private String searchName;

    public static SearchCategoryFragment newInstance(String name) {

        Bundle args = new Bundle();
        args.putString("name", name);
        SearchCategoryFragment fragment = new SearchCategoryFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_search_category);
        rvSearchCategory = findViewById(R.id.rv_search_category);
        srlRefresh = findViewById(R.id.srl_refresh);
    }

    @Override
    protected void initData() {
        if (getArguments() != null) {
            searchName = getArguments().getString("name");
        }

        rvSearchCategory.setLayoutManager(new LinearLayoutManager(getActivity()));
        rvSearchCategory.setAdapter(adapter = new BaseAdapter<MovieBean>() {
            @Override
            public BaseViewHolder onCreate(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.item_search_category, parent, false);
                return new BaseViewHolder(itemView);
            }

            @Override
            public void onBind(BaseViewHolder viewHolder, int RealPosition, MovieBean data) {
                ImageView ivMovieImg = viewHolder.findViewById(R.id.iv_movie_img);
                TextView tvMovieTitle = viewHolder.findViewById(R.id.tv_movie_title);
                TextView tvMovieYear = viewHolder.findViewById(R.id.tv_movie_year);
                ScoreView svScoreBox = viewHolder.findViewById(R.id.sv_score_box);
                TextView tvMovieInfo = viewHolder.findViewById(R.id.tv_movie_info);

                GlideUtil.loadRound(ivMovieImg, data.getCoverUrl(), 4);
                tvMovieTitle.setText(data.getName());
                tvMovieYear.setText(data.getVideoYear());
                svScoreBox.setScore(data.getDoubanScore());
                tvMovieInfo.setText(AppUtil.getMovieTagListStr(data, false));
            }
        });

        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<MovieBean>() {
            @Override
            public void onItemClick(int position, MovieBean data) {
                Intent intent = new Intent(getActivity(), PlayNewActivity.class);
                intent.putExtra(Contents.INTENT_MOVIE_DETAIL, data);
                startActivity(intent);
            }
        });

        View bottomView = LayoutInflater.from(getActivity()).inflate(R.layout.item_no_more_data, null, false);
        bottomTextView = bottomView.findViewById(R.id.tv_no_more_data_text);
        adapter.setBottomView(bottomView);

        rvSearchCategory.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (UiUtil.isSlideToBottom(recyclerView)) {
                    loadData(false);
                }
            }
        });

        srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData(true);
            }
        });

        loadData(true);
    }

    public void setSearchName(String searchName) {
        if (searchName != null) {
            this.searchName = searchName;
            loadData(true);
        }
    }

    @Override
    public void showLoading() {
        srlRefresh.setRefreshing(true);
        if (bottomTextView != null) {
            bottomTextView.setText(getString(R.string.list_load_more_data));
        }
    }

    @Override
    public void hideLoading() {
        srlRefresh.setRefreshing(false);
    }

    private void loadData(boolean isFirst) {
        if (searchName == null) {
            return;
        }
        if (isFirst) {
            pageNumber = 1;
        } else {
            pageNumber++;
        }
        if (presenter != null) {
            LoggerUtil.e("加载数据:" + pageNumber);
            presenter.getMovieList(pageNumber, searchName);
        }
    }

    @Override
    protected void initPresenter() {
        presenter = new SearchCategoryPresenter();
        presenter.attachView(this);
    }

    @Override
    public void showMovieList(List<MovieBean> movieBeans) {
        LoggerUtil.e("设置数据:" + pageNumber);
        if (pageNumber == 1) {
            adapter.setDataList(movieBeans);
        } else {
            adapter.addData(movieBeans);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showNoMore() {
        bottomTextView.setText(getString(R.string.list_no_more_data));
    }
}
